﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;
using System.Drawing;
using TeamBuy.Common.ThumbnailHelper;
using System.Net.Mail;
using System.Net;
using System.Configuration;

namespace TeamBuy.Common
{
    public static class PublicFunction
    {
        public static string MD5(string userPwd)
        {
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            string userPwdMD5 = BitConverter.ToString(md5.ComputeHash(UTF8Encoding.Default.GetBytes(userPwd)), 4, 8);
            userPwdMD5 = userPwdMD5.Replace("-", "");
            return userPwdMD5;
        }
        public static byte[] StreamFile(string fileName)
        {
            FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
            byte[] ImageData = new byte[fs.Length];
            fs.Read(ImageData, 0, System.Convert.ToInt32(fs.Length));
            fs.Close();
            return ImageData;
        }

        public static byte[] ResizeImage(byte[] b, int nWidth, int nHeight)
        {
            MemoryStream ms = new MemoryStream(b);
            Image result = new Bitmap(nWidth, nHeight);
            Image thisb = Image.FromStream(ms);
            using (Graphics g = Graphics.FromImage((Image)result))
                g.DrawImage(thisb, 0, 0, nWidth, nHeight);
            MemoryStream ms2 = new MemoryStream();
            result.Save(ms2, System.Drawing.Imaging.ImageFormat.Jpeg);
            ms.Close();
            return ms2.ToArray();
        }

        public static string GenerateVisitTime(DateTime visitTime)
        {
            DateTime currentTime = DateTime.Now;
            string year = "";
            string month = "";
            string day = "";
            string time = "";
            //if (visitTime.Year != currentTime.Year)
            //{
            //    if (currentTime.Year - visitTime.Year == 1)
            //    {
            //        year = "去年";
            //    }
            //    else if (currentTime.Year - visitTime.Year == 2)
            //    {
            //        year = "前年";
            //    }
            //    else
            //    {
            //        year = "多年以前";
            //        return year;
            //    }
            //}

            if (visitTime.Year == currentTime.Year)
            {
                if (visitTime.Month == currentTime.Month)
                {
                    //time = " " + visitTime.Hour + ":" + visitTime.Minute;
                    time = visitTime.Hour + ":" + visitTime.Minute;
                    if (currentTime.Day == visitTime.Day)
                    {
                        year = "";
                        month = "";
                        day = "今天";
                    }
                    else if (currentTime.Day - visitTime.Day == 1)
                    {
                        year = "";
                        month = "";
                        day = "昨天";
                    }
                    else if (currentTime.Day - visitTime.Day == 2)
                    {
                        year = "";
                        month = "";
                        day = "前天";
                    }
                    else
                    {
                        time = "";
                        month = visitTime.Month + "月";
                        month = " " + visitTime.Month + "月";
                        day = visitTime.Day + "日";
                    }
                }
            }
            else
            {
                month = visitTime.Month + "月";
                month = " " + visitTime.Month + "月";
                day = visitTime.Day + "日";
            }


            return year + month + day + time;
        }

        ///DES加密
        public static string DESEncrypt(string pToEncrypt)
        {
            try
            {
                string sKey = "aaaaaa";
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
                des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
                des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();
                StringBuilder ret = new StringBuilder();
                foreach (byte b in ms.ToArray())
                {
                    ret.AppendFormat("{0:X2}", b);
                }
                ret.ToString();
                return ret.ToString();
            }
            catch
            {
                return "0";
            }

        }

        ///DES解密
        public static string DESDecrypt(string pToDecrypt)
        {
            try
            {
                string sKey = "aaaaaa";
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();

                byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
                for (int x = 0; x < pToDecrypt.Length / 2; x++)
                {
                    int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
                    inputByteArray[x] = (byte)i;
                }

                des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
                des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();

                StringBuilder ret = new StringBuilder();

                return System.Text.Encoding.Default.GetString(ms.ToArray());
            }
            catch
            {
                return "0";
            }
        }

        public static bool SendMailToUser(string senderAddress, string receiverAddress, string subject, string body, string smtpHost, int smtpPort, string smtpPassword)
        {
            MailMessage mailMessage = new MailMessage(senderAddress, receiverAddress);
            mailMessage.Subject = subject;
            mailMessage.IsBodyHtml = true;
            mailMessage.Body = body;

            SmtpClient smtpClient = new SmtpClient(smtpHost, smtpPort);
            //使用SSL加密连线
            smtpClient.EnableSsl = true;
            NetworkCredential networkCredential = new NetworkCredential(senderAddress, smtpPassword);
            smtpClient.Credentials = networkCredential;
            try
            {
                smtpClient.Send(mailMessage);
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static string GetCopyright()
        {
            return "吴慧锋TeamBuy程序版权所有，未经许可请勿用于商业途径！";
        }

        public static double GetTimeSpanToSecond(DateTime dateEnded, DateTime dateStarted)
        {
            TimeSpan ts = new TimeSpan();
            ts = dateEnded - dateStarted;
            return ts.TotalSeconds;
        }
    }

    public enum MessageTypes
    {
        Success,
        Error
    }
}